<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="搜索词" prop="searchWord">
        <el-input
          v-model="queryParams.searchWord"
          placeholder="请输入搜索词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="功能" prop="effect">
        <el-input
          v-model="queryParams.effect"
          placeholder="请输入功能"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="问题描述" prop="problemDesc">
        <el-input
          v-model="queryParams.problemDesc"
          placeholder="请输入问题描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="运营负责人" label-width="80" prop="operationManager">
        <el-input
          v-model="queryParams.operationManager"
          placeholder="请输入运营负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否反馈" prop="feedback">
        <el-select v-model="queryParams.feedback" placeholder="请选择是否反馈" clearable>
          <el-option
            v-for="dict in dict.type.feedback"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:problem:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:problem:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:problem:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:problem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="problemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="问题编号" align="center" prop="id" />
      <el-table-column label="搜索词" align="center" prop="searchWord" />
      <el-table-column label="店铺名" align="center" prop="shopName" />
      <el-table-column label="价格" align="center" prop="price" width="100" />
      <el-table-column label="功能" align="center" prop="effect" />
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="问题描述" align="center" prop="problemDesc" />
      <el-table-column label="是否反馈" align="center" prop="feedback" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.feedback" :value="scope.row.feedback"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleAddFeedback(scope.row)"
            v-hasPermi="['system:feedback:add']"
          >添加反馈</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleFeedbackView(scope.row)"
            v-hasPermi="['system:feedback:list']"
          >查看反馈</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:problem:edit']"
            v-if="scope.row.feedback == 0"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:problem:remove']"
            v-if="scope.row.feedback == 0"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改问题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="搜索词" label-width="100px" prop="searchWord">
          <el-input v-model="form.searchWord" placeholder="请输入搜索词" />
        </el-form-item>
        <el-form-item label="店铺名" label-width="100px" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名" />
        </el-form-item>
        <el-form-item label="价格" label-width="100px" prop="price">
          <el-input-number v-model="form.price" :min="0.00" :precision="2" :step="0.01" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="功能" label-width="100px" prop="effect">
          <el-input v-model="form.effect" placeholder="请输入功能" />
        </el-form-item>
        <el-form-item label="图片" label-width="100px" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="问题描述" label-width="100px" prop="problemDesc">
          <el-input v-model="form.problemDesc" placeholder="请输入问题描述" />
        </el-form-item>
        <el-form-item label="运营负责人" label-width="100px" prop="operationManager">
          <el-input v-model="form.operationManager" placeholder="请输入运营负责人" />
        </el-form-item>
        <!-- <el-form-item label="是否反馈" prop="feedback">
          <el-select v-model="form.feedback" placeholder="请选择是否反馈">
            <el-option
              v-for="dict in dict.type.feedback"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改问题对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="1500px" append-to-body>
      <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="问题编号" align="center" prop="problemId" />
      <el-table-column label="单号" align="center" prop="orderNo" />
      <el-table-column label="订单类型" align="center" prop="orderType" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.feedback_order_type" :value="scope.row.orderType"/>
        </template>
      </el-table-column>
      <el-table-column label="订单图" align="center" prop="orderImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.orderImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="反馈图" align="center" prop="feedbackImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.feedbackImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="货值" align="center" prop="goodsValue" />
      <el-table-column label="PP汇率" align="center" prop="ppExchangeRate" />
      <el-table-column label="PP手续费" align="center" prop="ppHandlingFee" />
      <el-table-column label="PP打款金额" align="center" prop="ppPaymentAmount" />
      <el-table-column label="实时汇率" align="center" prop="realTimeExchangeRate" />
      <el-table-column label="佣金" align="center" prop="brokerage" />
      <el-table-column label="应付款" align="center" prop="amountPayable" />
      <el-table-column label="返款日期" align="center" prop="refundDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运营负责人" align="center" prop="operationManager" />
      <el-table-column label="测评负责" align="center" prop="evaluationManager" />
      <!-- <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" /> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total1"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-dialog>

    <!-- 添加或修改反馈信息对话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="800px" append-to-body>
      <el-form ref="form2" :model="form2" :rules="rules2" label-width="80px">
        <el-form-item label="单号" label-width="100px" prop="orderNo">
          <el-input v-model="form2.orderNo" placeholder="请输入单号" />
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select v-model="form2.orderType" placeholder="请选择是否反馈">
            <el-option
              v-for="dict in dict.type.feedback_order_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单图" label-width="100px" prop="orderImage">
          <image-upload v-model="form2.orderImage"/>
        </el-form-item>
        <el-form-item label="反馈图" label-width="100px" prop="feedbackImage">
          <image-upload v-model="form2.feedbackImage"/>
        </el-form-item>
        <el-form-item label="货值" label-width="100px" prop="goodsValue">
          <el-input-number v-model="form2.goodsValue" :min="0.00" :precision="2" :step="0.01" @input="handleGoodsValueInput" placeholder="请输入货值" />
        </el-form-item>
        <el-form-item label="PP汇率(%)" label-width="100px" prop="ppExchangeRate">
          <el-input-number v-model="form2.ppExchangeRate" :precision="2" :step="0.01" disabled placeholder="请输入PP汇率" />
        </el-form-item>
        <el-form-item label="PP手续费" label-width="100px" prop="ppHandlingFee">
          <el-input-number v-model="form2.ppHandlingFee" :precision="2" :step="0.01" disabled placeholder="请输入PP手续费" />
        </el-form-item>
        <el-form-item label="PP打款金额" label-width="100px" prop="ppPaymentAmount">
          <el-input-number v-model="form2.ppPaymentAmount" :precision="2" :step="0.01" disabled placeholder="请输入PP打款金额" />
        </el-form-item>
        <el-form-item label="实时汇率" label-width="100px" prop="realTimeExchangeRate">
          <el-input-number v-model="form2.realTimeExchangeRate" :min="0.00" :precision="2" :step="0.01" @input="handleRealTimeExchangeRateInput" placeholder="请输入实时汇率" />
        </el-form-item>
        <el-form-item label="佣金" label-width="100px" prop="brokerage">
          <el-input-number v-model="form2.brokerage" :min="0.00" :precision="2" :step="0.01" @input="handleRealTimeExchangeRateInput" placeholder="请输入实时汇率" />
        </el-form-item>
        <el-form-item label="应付款" label-width="100px" prop="amountPayable">
          <el-input-number v-model="form2.amountPayable" :precision="4" :step="0.01" disabled placeholder="请输入应付款" />
        </el-form-item>
        <el-form-item label="返款日期" label-width="100px" prop="refundDate">
          <el-date-picker clearable
            v-model="form2.refundDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择返款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="运营负责人" label-width="100px" prop="operationManager">
          <el-input v-model="form2.operationManager" disabled placeholder="请输入运营负责人" />
        </el-form-item>
        <el-form-item label="测评负责" label-width="100px" prop="evaluationManager">
          <el-input v-model="form2.evaluationManager" placeholder="请输入测评负责" />
        </el-form-item>
        <el-form-item label="备注" label-width="100px" prop="remark">
          <el-input v-model="form2.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormFeedback">确 定</el-button>
        <el-button @click="cancelFeedbackDialog">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listProblem, noPagelistProblem, getProblem, delProblem, addProblem, updateProblem } from "@/api/system/problem";
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback } from "@/api/system/feedback";

export default {
  name: "Problem",
  dicts: ['feedback','feedback_order_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题表格数据
      problemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      total1: 0,
      // 弹出层标题
      title1: "",
      title2: "",
      // 是否显示弹出层
      open1: false,
      // 是否显示弹出层
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchWord: null,
        shopName: null,
        price: null,
        effect: null,
        image: null,
        problemDesc: null,
        operationManager: null,
        feedback: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        searchWord: [
          { required: true, message: "搜索词不能为空", trigger: "blur" }
        ],
        shopName: [
          { required: true, message: "店铺名不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        effect: [
          { required: true, message: "功能不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        problemDesc: [
          { required: true, message: "问题描述不能为空", trigger: "blur" }
        ],
        operationManager: [
          { required: true, message: "运营负责人不能为空", trigger: "blur" }
        ]
      },
      // 表单参数
      form2: {},
      // 表单校验
      rules2: {
        problemId: [
          { required: true, message: "问题编号不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "单号不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "blur" }
        ],
        orderImage: [
          { required: true, message: "订单图不能为空", trigger: "blur" }
        ],
        feedbackImage: [
          { required: true, message: "反馈图不能为空", trigger: "blur" }
        ],
        goodsValue: [
          { required: true, message: "货值不能为空", trigger: "blur" }
        ],
        ppExchangeRate: [
          { required: true, message: "PP汇率不能为空", trigger: "blur" }
        ],
        ppHandlingFee: [
          { required: true, message: "PP手续费不能为空", trigger: "blur" }
        ],
        ppPaymentAmount: [
          { required: true, message: "PP打款金额不能为空", trigger: "blur" }
        ],
        realTimeExchangeRate: [
          { required: true, message: "实时汇率不能为空", trigger: "blur" }
        ],
        amountPayable: [
          { required: true, message: "应付款不能为空", trigger: "blur" }
        ],
        refundDate: [
          { required: true, message: "返款日期不能为空", trigger: "blur" }
        ],
        operationManager: [
          { required: true, message: "运营负责人不能为空", trigger: "blur" }
        ]
      },
      feedbackList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询问题列表 */
    getList() {
      this.loading = true;
      listProblem(this.queryParams).then(response => {
        this.problemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelFeedbackDialog() {
      this.open2 = false;
      this.reset2();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        searchWord: null,
        shopName: null,
        price: null,
        effect: null,
        image: null,
        problemDesc: null,
        operationManager: null,
        feedback: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    // 表单重置
    reset2() {
      this.form2 = {
        id: null,
        problemId: null,
        orderNo: null,
        orderType: null,
        orderImage: null,
        feedbackImage: null,
        goodsValue: null,
        ppPaymentAmount: null,
        realTimeExchangeRate: null,
        brokerage: null,
        amountPayable: null,
        refundDate: null,
        operationManager: null,
        evaluationManager: null,
        ppExchangeRate: null,
        ppHandlingFee: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form2");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProblem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改问题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProblem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProblem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问题编号为"' + ids + '"的数据项？').then(function() {
        return delProblem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 添加反馈按钮操作 */
    handleAddFeedback(row) {
      const problemId = row.id;
      this.reset2();
      this.form2.ppExchangeRate = 4.4;
      this.form2.ppHandlingFee = 0.49;
      this.form2.problemId = problemId;
      this.form2.operationManager = row.operationManager;
      this.open2 = true;
      this.title2 = "添加反馈信息";
    },
    /** 提交按钮 */
    submitFormFeedback() {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          addFeedback(this.form2).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open2 = false;
              this.getList();
            });
        }
      });
    },
    /** 查询反馈按钮操作 */
    handleFeedbackView(row) {
      const id = row.id;
      this.resetForm("queryForm");
      listFeedback({
        problemId: id
      }).then(response => {
        this.feedbackList = response.rows;
        this.total1 = response.total;
        this.open1 = true;
        this.title1 = "反馈信息";
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/problem/export', {
        ...this.queryParams
      }, `问题信息_${new Date().getTime()}.xlsx`)
    },
    // 计算pp打款金额
    handleGoodsValueInput(){
      if(this.form2.goodsValue != null && this.form2.goodsValue > 0){
        const totalMoney = this.form2.goodsValue + this.form2.ppHandlingFee;
        const exchangeRate = 1 - this.form2.ppExchangeRate / 100;
        this.form2.ppPaymentAmount = totalMoney / exchangeRate;
      }
    },
    // 计算应付款金额
    handleRealTimeExchangeRateInput(){
      if(this.form2.ppPaymentAmount != null && this.form2.ppPaymentAmount >= 0 && this.form2.realTimeExchangeRate != null && this.form2.realTimeExchangeRate >= 0){
        const amountPayable = this.form2.ppPaymentAmount * this.form2.realTimeExchangeRate + this.form2.brokerage;
        this.form2.amountPayable = amountPayable.toFixed(4);
      }
    }
  }
};
</script>
